package com.gw.quotes.net.service;

import org.ksoap2.serialization.SoapObject;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.gw.quotes.entity.business.OrderListEntity;
import com.gw.quotes.net.NewException;
import com.gw.quotes.net.Request;
import com.gw.quotes.net.Response;

public class OrderlistQueryServiceImpl extends WebBaseServiceImpl {

	@Override
	public Response setResponse() {
		return new Response();
	}

	@Override
	public void checkParam(Request request) throws NewException {
		if (request == null) {
			throw new NewException(Response.RESULT_ERROR, "传入request不能为空");
		}
		if (checkNull(request.getLoginname())) {
			throw new NewException(Response.RESULT_ERROR, "用户登录名不能为空");
		}
		if (checkNull(request.getTicket())) {
			throw new NewException(Response.RESULT_ERROR, "接口凭据不能为空");
		}
	}

	@Override
	public void setParamByRequest(Request request, SoapObject rpc)
			throws NewException {
		rpc.addProperty("arg0", request.getLoginname());
		rpc.addProperty("arg1", request.getTicket());
		rpc.addProperty("arg2", request.getJsonconditions());
	}

	@Override
	public void resolveResponse(Object object, Response response)
			throws NewException {
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
				.create();
		java.lang.reflect.Type type = new TypeToken<OrderListEntity>() {
		}.getType();
		OrderListEntity result = gson.fromJson(object.toString(), type);
		response.setMsg(result.getMsg());
		response.setResult(result.getResult());
		if ("凭据错误".equals(response.getMsg())) {
			response.setMsg("凭据错误，请重新登录");
		}
		if (response.getResult() == Response.RESULT_SUCCESS) {
			response.setObj(result.getOrderlist());
			System.out.println(response.toString());
		}
		Log.i("xd", "object" + object.toString());
		System.out.println("object" + object.toString());
	}

	@Override
	public String getMethodName() throws NewException {
		return "getOrderlist";
	}

}
